package com.gh.demo.controller;


import com.gh.demo.entity.GoodSelect;
import com.gh.demo.entity.Goods;
import com.gh.demo.entity.GoodsType;
import com.gh.demo.mapper.Receivemapper;
import com.gh.demo.mapper.Receivemappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


@Controller
public class testcontroller {


    @Autowired
    private Receivemapper receivemapper;
    @Autowired
    private Receivemappers receivemappers;

    //首页打开
    @RequestMapping("/")
    public String login(Model model) {
        model.addAttribute("types",receivemappers.selectWar());
        return "login";
    }

    //领取信息登记
    @PostMapping(value = "/insert")
    public String insert(@RequestParam("name") String name,
                         @RequestParam("type") String type,
                         @RequestParam("number") Integer number,
                         @RequestParam("date") String date, Model model
                         ) {


        Goods goods = new Goods();
        goods.setName(name);
        goods.setType(type);
        goods.setNumber(number);
        goods.setDate(date);
        receivemapper.save(goods);
        receivemappers.updates(number, type);//修改仓库总数
        //  System.out.println(receivemapper.selectNew().toString());
        model.addAttribute("list", receivemapper.selectNew());
        model.addAttribute("types",receivemappers.selectWar());

        return "login";
    }


    //统计
    @RequestMapping("/count")
    public String count(Model model) {

        model.addAttribute("count", s());
        model.addAttribute("types",receivemappers.selectWar());
        return "login";

    }


    //List(Object)转换List(GoodSelect)
    public List<GoodSelect> s() {
        List<GoodSelect> list = new ArrayList<>();
        List _list = receivemapper.selectCount();
        for (Object row : _list) {
            Object[] cells = (Object[]) row;
            GoodSelect goodSelect = new GoodSelect();
            goodSelect.setName((String) cells[0]);
            goodSelect.setType((String) cells[1]);
            goodSelect.setNumber(Integer.parseInt(cells[2].toString()));
            list.add(goodSelect);
        }
        return list;
    }
    /*
    仓库添加
    * */
    @PostMapping("/inserts")
    public String inserts(@RequestParam("id") Integer id,
                          @RequestParam("type") String type,
                          @RequestParam("numbers") Integer numbers, Model model) {
        GoodsType goodsType = new GoodsType();
        goodsType.setId(id);
        goodsType.setType(type);
        goodsType.setNumbers(numbers);
        receivemappers.save(goodsType);
        model.addAttribute("types",receivemappers.selectWar());

        return "login";
    }

    //仓库查看
    @RequestMapping("/show")
    public String show(Model model) {
        model.addAttribute("show", receivemappers.selectWar());
        model.addAttribute("types",receivemappers.selectWar());
        return "login";
    }

}
